package com.tosit.bmx.controller.admin;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.SecondSub;
import com.tosit.bmx.domian.SecondType;
import com.tosit.bmx.domian.SencondSubVo;
import com.tosit.bmx.domian.Subject;
import com.tosit.bmx.service.admin.CarManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 套题管理类
 *
 * @author tsl
 * @create 2018-04-24 15:27
 **/
@RequestMapping()
@Controller
public class SecondtypeController {
    @Autowired
    CarManagerService carManagerService;

    /**
     * 添加一个套题
     *
     * @param secondType
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/secondType", method = RequestMethod.POST)
    public void addSecondType(SecondType secondType, @RequestParam("courseImg") MultipartFile file , HttpServletRequest request, HttpServletResponse response) {
        Integer result=null;
        if(!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/ico/");
            //上传文件名
            String filename = file.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                file.transferTo(new File(path + File.separator + newFileName));
                secondType.setImgpath("/upload/ico/"+newFileName);
                result =  carManagerService.addSecondType(secondType);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除一个套题
     *
     * @param id
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/secondType", method = RequestMethod.DELETE)
    public void removeSecondType(Integer id, HttpServletResponse response) {
        Integer result = carManagerService.removeSecondType(id);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除一个或多个套题
     *
     * @param ids
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/secondTypes", method = RequestMethod.DELETE)
    public void removeSecondTypes(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = carManagerService.removeSecondTypes(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 修改一个套题
     *
     * @param secondType
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @RequestMapping(value = "/secondType/fix", method = RequestMethod.POST)
    public void modifySecondType(SecondType secondType,  @RequestParam("courseImg") MultipartFile file , HttpServletRequest request,HttpServletResponse response) {
        Integer result=null;
        if(!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/ico/");
            //上传文件名
            String filename = file.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                file.transferTo(new File(path + File.separator + newFileName));
                secondType.setImgpath("/upload/ico/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
         result = carManagerService.modifySecondType(secondType);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     *  复制一个或多个套题到另一个或几个题型里(其实就是添加对应的套题，并绑定到指定的题型中,并为添加的套题绑定题目)
     * @author tsl
     * @date 2018/4/25 9:40
     * @param secondTypes list(id(复制套题的id),name,exid,imgpath,px)
     * @return
     */
    @RequestMapping(value = "/copySecondTypes", method = RequestMethod.POST)
    public void copySecondTypesToExer(@RequestBody List<SecondType> secondTypes, HttpServletResponse response) {
        Integer result = carManagerService.copySecondTypes(secondTypes);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     *  复制一个或多个套题到另一个套题里
     * @author tsl
     * @date 2018/4/25 9:40
     * @param sencondSubVos SencondSubVo(copyId(复制套题的id)的集合,copyId(复制到某套题的id))
     * @return
     */
    @RequestMapping(value = "/copySecondTypes/secondType", method = RequestMethod.POST)
    public void copySecondTypesToSelf(@RequestBody SencondSubVo sencondSubVos, HttpServletResponse response) {
        Integer result = carManagerService.copySecondTypesToSelf(sencondSubVos);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 删除一个或多个套题和题的关联
     * @author tsl
     * @date 2018/4/25 14:56
     * @param ids 关联对象id
     * @return
     */
    @RequestMapping(value = "/secondType/relateSub", method = RequestMethod.DELETE)
    public void relateSub(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = carManagerService.deleterelateSubs(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     *
     * 修改一个或多个套题和题的关联的排序
     * @author tsl
     * @date 2018/4/25 14:56
     * @param secondSubs （id，sorder）
     * @return
     */
    @RequestMapping(value = "/secondType/relateSub", method = RequestMethod.PUT)
    public void relateSubUpdate(@RequestBody List<SecondSub> secondSubs, HttpServletResponse response) {
        Integer result =carManagerService.modifyRelateSubs(secondSubs);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 添加一个或多个套题和题的关联
     * @author tsl
     * @date 2018/4/25 14:56
     * @param secondSubs 添加关联对象（seid，sid，sorder）集合
     * @return
     */
    @RequestMapping(value = "/secondType/relateSub", method = RequestMethod.POST)
    public void addRelateSub(@RequestBody SecondSub[] secondSubs, HttpServletResponse response) {
       Integer result = carManagerService.addRelateSub(new ArrayList<SecondSub>(Arrays.asList(secondSubs)));
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    private class SecondSubDemo{
        private List<SecondSub> secondSubs;

        public List<SecondSub> getSecondSubs() {
            return secondSubs;
        }

        public void setSecondSubs(List<SecondSub> secondSubs) {
            this.secondSubs = secondSubs;
        }
    }

}
